<template>
  <div className="app-container">
    <div className="table-container">
      <base-list
        ref="table"
        url="/jointly_read_activity"
        :options="options"
        :columns="columns"
        :format="format"
        :rules="rules"
        permission="ADMIN"
        @reloadOptions="handleGetOptions"
      >
        <template slot="header_actions" slot-scope="scope">
          <el-popconfirm
            title="是否刷新ES？"
            icon="el-icon-finished"
            icon-color="orange"
            confirm-button-type="warning"
            @confirm="refreshEs()">
            <el-button slot="reference" v-waves type="warning" icon="el-icon-refresh" style="margin-right: 10px">刷新ES</el-button>
          </el-popconfirm>
        </template>
      </base-list>
    </div>
  </div>
</template>

<script>
import {requestBase} from '@/api/common'
import BaseList from '@/components/BaseList/index.vue'
import permission from '@/directive/permission' // 权限判断指令
import waves from '@/directive/waves' // waves directive

export default {
  name: 'JointlyReadActivity',
  components: {BaseList},
  directives: { permission, waves },
  data() {
    return {
      columns: [
        {show: true, edit: false, width: '100', key: 'id', fixed: true, label: 'ID', tooltip: 'ID', type: 'NUMBER', queryable: true, sortable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'name', label: '共读名称', tooltip: '共读名称', type: 'WORD', queryable: true, sortable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'initiator', label: '创建人', tooltip: '创建人', type: 'REMOTE_SELECT', remote: {url: '/user_info', label: 'nickName', multiple: false, queryable: true}},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'books', label: '共读图书', tooltip: '共读图书', type: 'REMOTE_SELECT_BOOK', remote: {multiple: true}},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'hotValue', label: '热度', tooltip: '热度', type: 'NUMBER', queryable: true, sortable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'maxUserNum', label: '人数限制', tooltip: '人数限制', type: 'NUMBER', queryable: true, sortable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'expense', label: '报名费', tooltip: '报名费', type: 'NUMBER', queryable: true, sortable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'readStartDate', label: '阅读活动开始日期', tooltip: '阅读活动开始日期', type: 'DATE_TIME', queryable: true, sortable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'readEndDate', label: '阅读活动结束日期', tooltip: '阅读活动结束日期', type: 'DATE_TIME', queryable: true, sortable: true},
        {show: false, edit: true, width: 'auto',minWidth: '200', key: 'recommend', label: '推荐语', tooltip: '推荐语', type: 'HTML'},
        {show: true, edit: false, width: 'auto',minWidth: '200', key: 'sectionNum', label: '章节数量', tooltip: '章节数量', type: 'WORD', sortable: true},
        {show: false, edit: false, width: 'auto',minWidth: '200', key: 'createTime', label: '创建时间', tooltip: '创建时间', type: 'DATE_TIME'},
        {show: true, edit: false, width: 'auto',minWidth: '200', key: 'updateTime', label: '更新时间', tooltip: '更新时间', type: 'DATE_TIME', queryable: true},
        {show: false, edit: false, width: 'auto',minWidth: '200', key: 'createUser', label: '创建人', tooltip: '创建人', type: 'WORD'},
        {show: false, edit: false, width: 'auto',minWidth: '200', key: 'updateUser', label: '修改人', tooltip: '修改人', type: 'WORD'},
      ],
      format: {
        books: (data, index) => {
          return (data.books || []).map(e => e.name).join(',')
        },
        initiator: (data, index) => {
          return data.initiator ? data.initiator.nickName + '('+data.initiator.id+')' : ''
        },
        createUser: (data, index) => {
          return data.createUser ? data.createUser.nickName + '('+data.createUser.id+')' : ''
        },
        updateUser: (data, index) => {
          return data.updateUser ? data.updateUser.nickName + '('+data.updateUser.id+')' : ''
        }
      },
      rules: {
        add: {
          name: [{required: true, message: '共读名称必填!', trigger: 'blur'}],
          initiator: [{required: true, message: '创建人必填!', trigger: 'change'}],
          books: [{required: true, message: '共读图书必填!', trigger: 'change'}],
          signUpStartDate: [{required: true, message: '报名开始日期必填!', trigger: 'blur'}],
          signUpEndDate: [{required: true, message: '报名结束日期必填!', trigger: 'blur'}],
          readStartDate: [{required: true, message: '阅读活动开始日期必填!', trigger: 'blur'}],
          readEndDate: [{required: true, message: '阅读活动结束日期必填!', trigger: 'blur'}],
          recommend: [{required: true, message: '推荐语必填!', trigger: 'blur'}],
        },
        edit: {
          id: [{required: true, message: '请选择数据!', trigger: 'change'}],
          name: [{required: true, message: '共读名称必填!', trigger: 'blur'}],
          initiator: [{required: true, message: '创建人必填!', trigger: 'change'}],
          books: [{required: true, message: '共读图书必填!', trigger: 'change'}],
          signUpStartDate: [{required: true, message: '报名开始日期必填!', trigger: 'blur'}],
          signUpEndDate: [{required: true, message: '报名结束日期必填!', trigger: 'blur'}],
          readStartDate: [{required: true, message: '阅读活动开始日期必填!', trigger: 'blur'}],
          readEndDate: [{required: true, message: '阅读活动结束日期必填!', trigger: 'blur'}],
          recommend: [{required: true, message: '推荐语必填!', trigger: 'blur'}],
        }
      },
      options: {
      }
    }
  },

  created() {
    this.handleGetOptions()
  },
  mounted() {
  },
  methods: {
    refreshEs() {
      requestBase({
        url: '/jointly_read_activity/reload_es',
        method: 'POST'
      }).then(res => {
        this.$notify.success('刷新成功!')
      })

    },
    handleGetOptions() {
      const options = JSON.parse(localStorage.getItem('allOptions'))
      if (options) {
        this.options = Object.assign(this.options, options)
      }
    }
  }
}
</script>


